package id.co.rho.scoring.cid.server.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Component;

import id.co.rho.scoring.cid.domain.Model;
import id.co.rho.scoring.cid.server.dao.ModelSearchDao;

@Component
public class ModelSearchDaoImpl implements ModelSearchDao {

	@PersistenceContext
	EntityManager entityManager;

	@Override
	public List<Model> searchCriteria(String name, String type, Integer revision) {
		return entityManager
				.createQuery(
						"from Model s where lower(s.modelName) like :name and lower(s.typeModelConfig) like :type and s.revision=:revision")
				.setParameter("name", '%' + name.toLowerCase() + '%')
				.setParameter("type", '%' + type.toLowerCase() + '%')
				.setParameter("revision", revision).getResultList();
	}

	@Override
	public List<Model> searchCriteria(String name, String type) {
		return entityManager
				.createQuery(
						"from Model s where lower(s.modelName) like :name and lower(s.typeModelConfig) like :type")
				.setParameter("name", '%' + name.toLowerCase() + '%')
				.setParameter("type", '%' + type.toLowerCase() + '%')
				.getResultList();
	}

	@Override
	public List<Model> searchCriteria(String name, Integer revision) {
		return entityManager
				.createQuery(
						"from Model s where lower(s.modelName) like :name and s.revision=:revision")
				.setParameter("name", '%' + name.toLowerCase() + '%')
				.setParameter("revision", revision).getResultList();
	}

	@Override
	public List<Model> searchCriteria(String name) {
		return entityManager
				.createQuery("from Model s where lower(s.modelName) like :name")
				.setParameter("name", '%' + name.toLowerCase() + '%')
				.getResultList();
	}

	@Override
	public List<Model> searchCriteria(String name, String type,
			Integer revision, Boolean active) {
		return entityManager
				.createQuery(
						"from Model s where lower(s.modelName) like :name and lower(s.typeModelConfig) like :type and s.revision=:revision and s.aktifStatus=:active")
				.setParameter("name", '%' + name.toLowerCase() + '%')
				.setParameter("type", '%' + type.toLowerCase() + '%')
				.setParameter("revision", revision)
				.setParameter("active", active).getResultList();
	}

	@Override
	public List<Model> searchCriteria(String name, String type, Boolean active) {
		return entityManager
				.createQuery(
						"from Model s where lower(s.modelName) like :name and lower(s.typeModelConfig) like :type and s.aktifStatus=:active")
				.setParameter("name", '%' + name.toLowerCase() + '%')
				.setParameter("type", '%' + type.toLowerCase() + '%')
				.setParameter("active", active).getResultList();
	}

	@Override
	public List<Model> searchCriteria(String name, Integer revision,
			Boolean active) {
		return entityManager
				.createQuery(
						"from Model s where lower(s.modelName) like :name and s.revision=:revision and s.aktifStatus=:active")
				.setParameter("name", '%' + name.toLowerCase() + '%')
				.setParameter("revision", revision)
				.setParameter("active", active).getResultList();
	}

	@Override
	public List<Model> searchCriteria(String name, Boolean active) {
		return entityManager
				.createQuery(
						"from Model s where lower(s.modelName) like :name and s.aktifStatus=:active")
				.setParameter("name", '%' + name.toLowerCase() + '%')
				.setParameter("active", active).getResultList();
	}
}
